package com.zh.work.service.platform.trans.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zh.work.dao.platform.trans.ErrorTransMapper;
import com.zh.work.dao.platform.trans.TransBackupMapper;
import com.zh.work.dao.platform.trans.TransRealMapper;
import com.zh.work.entity.trans.TransBackupEntity;
import com.zh.work.entity.trans.TransRealEntity;
import com.zh.work.entity.trans.TransTodayEntity;
import com.zh.work.service.platform.trans.ErrorTransService;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * code is far away from bug with the animal protecting
 * ┏┓　　　┏┓
 * ┏┛┻━━━┛┻┓
 * ┃　　　　　　　┃
 * ┃　　　━　　　┃
 * ┃　┳┛　┗┳　┃
 * ┃　　　　　　　┃
 * ┃　　　┻　　　┃
 * ┃　　　　　　　┃
 * ┗━┓　　　┏━┛
 * 　　┃　　　┃神兽保佑
 * 　　┃　　　┃代码无BUG！
 * 　　┃　　　┗━━━┓
 * 　　┃　　　　　　　┣┓
 * 　　┃　　　　　　　┏┛
 * 　　┗┓┓┏━┳┓┏┛
 * 　　　┃┫┫　┃┫┫
 * 　　　┗┻┛　┗┻┛
 *
 * @Description: 异常交易业务实现层
 * @Author: Li.HuiNeng
 * @Create in: 2020-01-07 17:17
 * @Version: 1.0.1
 */
@Service
public class ErrorTransServiceImpl extends ServiceImpl<ErrorTransMapper, TransTodayEntity> implements ErrorTransService {

    // 今日异常交易
    @Autowired
    private ErrorTransMapper errorMapper;

    // 历史异常交易
    @Autowired
    private TransBackupMapper backupMapper;

    // 冲正异常交易
    @Autowired
    private TransRealMapper realMapper;

    @Override
    public IPage<TransTodayEntity> getErrorTransTodayPages(Page<TransTodayEntity> page, TransTodayEntity param) {
        return errorMapper.selectPage(page, new LambdaQueryWrapper<TransTodayEntity>()
                .between(ObjectUtils.allNotNull(param.getTransStartTime(), param.getTransEndTime()),
                        TransTodayEntity::getTransDate, param.getTransStartTime(), param.getTransEndTime())
                .between(ObjectUtils.allNotNull(param.getStartDateTime(), param.getEndDateTime()),
                        TransTodayEntity::getSettleDate, param.getStartDateTime(), param.getEndDateTime())
                .between(ObjectUtils.allNotNull(param.getAmtStart(), param.getAmtEnd()),
                        TransTodayEntity::getAmount, param.getAmtStart(), param.getAmtEnd())
                .eq(Objects.nonNull(param.getTransModel()), TransTodayEntity::getTransModel, param.getTransModel())
                .eq(Objects.nonNull(param.getTransStatus()), TransTodayEntity::getTransStatus, param.getTransStatus())
                .eq(Objects.nonNull(param.getLiquidator()), TransTodayEntity::getLiquidator, param.getLiquidator())
                .eq(Objects.nonNull(param.getBranchNo()), TransTodayEntity::getBranchNo, param.getBranchNo())
                .like(StringUtils.isNotEmpty(param.getMerchNo()), TransTodayEntity::getMerchNo, param.getMerchNo())
                .like(StringUtils.isNotEmpty(param.getMerchName()), TransTodayEntity::getMerchName, param.getMerchName())
                .like(StringUtils.isNotEmpty(param.getTermNo()), TransTodayEntity::getTermNo, param.getTermNo())
                .like(StringUtils.isNotEmpty(param.getCardNo()), TransTodayEntity::getCardNo, param.getCardNo())
                .like(StringUtils.isNotEmpty(param.getRefNo()), TransTodayEntity::getRefNo, param.getRefNo())
                .in(TransTodayEntity::getRespCode, Arrays.asList(96, 98))
                .or()
                .isNull(TransTodayEntity::getRespCode)
        );
    }

    @Override
    public IPage<TransBackupEntity> getTransBackupTodayPages(Page<TransBackupEntity> page, TransBackupEntity param) {
        return backupMapper.selectPage(page, new LambdaQueryWrapper<TransBackupEntity>()
                .between(ObjectUtils.allNotNull(param.getTransStartTime(), param.getTransEndTime()),
                        TransBackupEntity::getTransDate, param.getTransStartTime(), param.getEndDateTime())
                .between(ObjectUtils.allNotNull(param.getStartDateTime(), param.getEndDateTime()),
                        TransBackupEntity::getSettleDate, param.getStartDateTime(), param.getEndDateTime())
                .between(ObjectUtils.allNotNull(param.getAmtStart(), param.getAmtEnd()),
                        TransBackupEntity::getAmount, param.getAmtStart(), param.getAmtEnd())
                .eq(Objects.nonNull(param.getTransModel()), TransBackupEntity::getTransModel, param.getTransModel())
                .eq(Objects.nonNull(param.getTransStatus()), TransBackupEntity::getTransStatus, param.getTransStatus())
                .eq(Objects.nonNull(param.getLiquidator()), TransBackupEntity::getLiquidator, param.getLiquidator())
                .eq(Objects.nonNull(param.getBranchNo()), TransBackupEntity::getBranchNo, param.getBranchNo())
                .like(StringUtils.isNotEmpty(param.getMerchNo()), TransBackupEntity::getMerchNo, param.getMerchNo())
                .like(StringUtils.isNotEmpty(param.getMerchName()), TransBackupEntity::getMerchName, param.getMerchName())
                .like(StringUtils.isNotEmpty(param.getTermNo()), TransBackupEntity::getTermNo, param.getTermNo())
                .like(StringUtils.isNotEmpty(param.getCardNo()), TransBackupEntity::getCardNo, param.getCardNo())
                .like(StringUtils.isNotEmpty(param.getRefNo()), TransBackupEntity::getRefNo, param.getRefNo())
                .in(TransBackupEntity::getRespCode, Arrays.asList(96, 98))
                .or()
                .isNull(TransBackupEntity::getRespCode)
        );
    }

    @Override
    public IPage<TransRealEntity> getTransRealPages(Page<TransRealEntity> page, TransRealEntity param) {
        QueryWrapper<TransRealEntity> wrapper = new QueryWrapper<TransRealEntity>();
        wrapper.select("id", "trans_date", "trans_time", "type_code", "code_memo", "merchno",
                "termno", "cardno", "amount", "batchno",
                "traceno", "refno", "resp_code", "card_organ", "ip", "address", "merch_name", "branchno", "branch_name",
                "agentno", "agent_name", "first_agentno", "first_agent_name");
        return realMapper.selectPage(page, wrapper.lambda()
                .between(ObjectUtils.allNotNull(param.getTransStartTime(), param.getTransEndTime()),
                        TransBackupEntity::getTransDate, param.getTransStartTime(), param.getTransEndTime())
                .between(ObjectUtils.allNotNull(param.getStartDateTime(), param.getEndDateTime()),
                        TransBackupEntity::getSettleDate, param.getStartDateTime(), param.getEndDateTime())
                .between(ObjectUtils.allNotNull(param.getAmtStart(), param.getAmtEnd()),
                        TransBackupEntity::getAmount, param.getAmtStart(), param.getAmtEnd())
                .eq(Objects.nonNull(param.getTransModel()), TransBackupEntity::getTransModel, param.getTransModel())
                .eq(Objects.nonNull(param.getTransStatus()), TransBackupEntity::getTransStatus, param.getTransStatus())
                .eq(Objects.nonNull(param.getLiquidator()), TransBackupEntity::getLiquidator, param.getLiquidator())
                .eq(Objects.nonNull(param.getBranchNo()), TransBackupEntity::getBranchNo, param.getBranchNo())
                .eq(Objects.nonNull(param.getTypeCode()), TransBackupEntity::getTypeCode, "4007")
                .like(StringUtils.isNotEmpty(param.getMerchNo()), TransBackupEntity::getMerchNo, param.getMerchNo())
                .like(StringUtils.isNotEmpty(param.getMerchName()), TransBackupEntity::getMerchName, param.getMerchName())
                .like(StringUtils.isNotEmpty(param.getTermNo()), TransBackupEntity::getTermNo, param.getTermNo())
                .like(StringUtils.isNotEmpty(param.getCardNo()), TransBackupEntity::getCardNo, param.getCardNo())
                .like(StringUtils.isNotEmpty(param.getRefNo()), TransBackupEntity::getRefNo, param.getRefNo())
        );
    }

    @Override
    public IPage<TransTodayEntity> getTransTodayPages(Page<TransTodayEntity> page, TransTodayEntity param) {
        return errorMapper.selectPage(page, new LambdaQueryWrapper<TransTodayEntity>()
                .between(ObjectUtils.allNotNull(param.getTransStartTime(), param.getTransEndTime()),
                        TransBackupEntity::getTransDate, param.getTransStartTime(), param.getTransEndTime())
                .between(ObjectUtils.allNotNull(param.getStartDateTime(), param.getEndDateTime()),
                        TransBackupEntity::getSettleDate, param.getStartDateTime(), param.getEndDateTime())
                .between(ObjectUtils.allNotNull(param.getAmtStart(), param.getAmtEnd()),
                        TransBackupEntity::getAmount, param.getAmtStart(), param.getAmtEnd())
                .eq(Objects.nonNull(param.getTransModel()), TransBackupEntity::getTransModel, param.getTransModel())
                .eq(Objects.nonNull(param.getTransStatus()), TransBackupEntity::getTransStatus, param.getTransStatus())
                .eq(Objects.nonNull(param.getLiquidator()), TransBackupEntity::getLiquidator, param.getLiquidator())
                .eq(Objects.nonNull(param.getBranchNo()), TransBackupEntity::getBranchNo, param.getBranchNo())
                .like(StringUtils.isNotEmpty(param.getMerchNo()), TransBackupEntity::getMerchNo, param.getMerchNo())
                .like(StringUtils.isNotEmpty(param.getMerchName()), TransBackupEntity::getMerchName, param.getMerchName())
                .like(StringUtils.isNotEmpty(param.getTermNo()), TransBackupEntity::getTermNo, param.getTermNo())
                .like(StringUtils.isNotEmpty(param.getCardNo()), TransBackupEntity::getCardNo, param.getCardNo())
                .like(StringUtils.isNotEmpty(param.getRefNo()), TransBackupEntity::getRefNo, param.getRefNo())
        );
    }

    @Override
    public Map<String, String> getErrorTransStatistic(TransTodayEntity param) {
        return errorMapper.getErrorTransStatistic(param);
    }

    @Override
    public List<Map<String, String>> getErrorTransRealStatistic(TransTodayEntity param) {
        return errorMapper.getErrorTransRealStatistic(param);
    }
}
